home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / science / cdrift3.zip / SUN.C < prev    next >
C/C++ Source or Header  |  1990-02-22  |  4KB  |  95 lines

  1. /* This program is Copyright (c) 1990 David Allen.  It may be freely
  2.    distributed as long as you leave my name and copyright notice on it.
  3.    I'd really like your comments and feedback; send e-mail to
  4.    davea@vlsi.ll.mit.edu, or send us-mail to David Allen, 10 O'Moore Ave,
  5.    Maynard, MA 01754. */
  6.  
  7.  
  8. /* This file contains code to run tec under Un*x and Sunview.  The
  9.    random number code is the same as that contained in unix.c; the
  10.    graphics code was inspired by the example code from Sun contained
  11.    in /usr/share/src/sun/suntool/examples/coloredit.c and in the
  12.    Sunview Programmer's Guide.  I don't claim to understand it.  The
  13.    code in draw() is similar to that in ami.c. */
  14.  
  15. #include <suntool/sunview.h>
  16. #include <suntool/canvas.h>
  17. #include <sys/types.h>
  18. #include <sys/timeb.h>
  19. #include "const.h"
  20. #include "var.h"
  21.  
  22. extern short step, phead; /* Defined in tec1.c */
  23. extern unsigned char t[2][MAXX][MAXY]; /* Defined in tec1.c */
  24.  
  25. /* These arrays define similar colors to those in ami.c; the first two
  26.    are black, followed by two blues, four greens, four reds, four purples,
  27.    four greys, and a bunch of bright whites. */
  28. unsigned char red [] = {
  29.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0xb0, 0xd0, 0xf0,
  30.    0x90, 0xb0, 0xd0, 0xf0, 0x90, 0xb0, 0xd0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  31.    0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  32. unsigned char green [] = {
  33.    0x00, 0x00, 0x00, 0x00, 0x90, 0xb0, 0xd0, 0xf0, 0x00, 0x00, 0x00, 0x00,
  34.    0x00, 0x00, 0x00, 0x00, 0x90, 0xb0, 0xd0, 0xf0, 0x90, 0xb0, 0xd0, 0xf0,
  35.    0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  36. unsigned char blue [] = {
  37.    0x00, 0x00, 0xd0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38.    0x90, 0xb0, 0xd0, 0xf0, 0x90, 0xb0, 0xd0, 0xf0, 0x00, 0x00, 0x00, 0x00,
  39.    0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
  40.  
  41.  
  42. Pixwin *pw;
  43. struct rect tr = { 0, 0, MAXX * 6, MAXY * 6 };
  44.  
  45.  
  46. tecdestroy (c, s) Notify_client c; Destroy_status s; { step = MAXSTEP; }
  47. /* A function called when the user tells Sunview to exit; it tells main()
  48. that its time is up, so main can exit after finishing the current step */
  49.  
  50.  
  51. main (argc, argv) int argc; char **argv; {
  52.    struct timeb t;
  53.    Frame  base_frame;
  54.    Canvas canvas;
  55.    char   cmsname[CMS_NAMESIZE];
  56.  
  57.    /* Initialize random number generator */
  58.    ftime (&t); srandom ((int) ((t.time % 100000) + t.millitm));
  59.  
  60.    /* Suntools-specific graphics setup */
  61.    base_frame = window_create (NULL, FRAME, FRAME_LABEL, "suntec", 
  62.       WIN_WIDTH, MAXX * 6, WIN_HEIGHT, MAXY*6, 0);
  63.    canvas = window_create (base_frame, CANVAS, WIN_WIDTH, 
  64.       MAXX * 6, WIN_HEIGHT, MAXY * 6, 0);
  65.    pw = (Pixwin *) canvas_pixwin (canvas);
  66.    sprintf (cmsname, "suntec%D", getpid());
  67.    pw_setcmsname (pw, cmsname);
  68.    pw_putcolormap (pw, 0, 32, red, green, blue);
  69.    notify_interpose_destroy_func (base_frame, tecdestroy);
  70.    window_set (base_frame, WIN_SHOW, TRUE, 0);
  71.  
  72.    /* Run the program, checking with Sunview after each step */
  73.    init (*++argv);
  74.    for (step=0; step<MAXSTEP; step++) { onestep (); notify_dispatch (); }
  75.    exit (0); }
  76.  
  77.  
  78. rnd (top) short top; { return (random () % top); }
  79.  
  80.  
  81. panic (s) char *s; { printf ("PANIC: %s\n", s); exit (1); }
  82.  
  83.  
  84. draw (src) short src; {
  85.    register short i, j, k, x;
  86.  
  87.    pw_lock (pw, &tr);
  88.    for (j=0, i=0; j<YSIZE; j++, i=0) {
  89.       top: x = t[src][i][j] >> 2;
  90.       k = i+1; while (((t[src][k][j] >> 2) == x) && (k < XSIZE-1)) k++;
  91.       pw_rop (pw, i*6, j*6, (k-i)*6, 6, PIX_SRC | PIX_COLOR (x), NULL, 0, 0);
  92.       i = k-1;
  93.       if (i < XSIZE-1) { i++; goto top; } }
  94.    pw_unlock (pw); }
  95.